import { setList, getList } from './const/subApps'
import { rewriteRouter } from './router/rewriteRouter'
import { currentApp } from './utils'
import { setMainLifeCycle } from './const/mainLifeCycle'
import { prefetch } from './loader/prefetch'
import { Custom } from './customevent'

const custom = new Custom()
window.custom = custom
rewriteRouter()
export const registerMicroApps = (appList,  lifeCycle) => {
    setList(appList)
    setMainLifeCycle(lifeCycle)
}

export const start = () => {
    const apps = getList()
    if (!apps.length) {
        // 子应用列表为空
        throw new Error('请先注册子应用')
    }
    // 有子应用，查找符合当前路由的子应用
    const app = currentApp()
    if (app) {
        const { pathname, hash } = window.location
        const url = `${pathname}${hash}`
        window.history.pushState({}, '', url)
        window.__CURRENT_SUB_APP__ = app.activeRule
    }
    // 预加载
    prefetch()
}
